package com.lujianfei.productprice.widget;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lujianfei.productprice.R;
import com.lujianfei.productprice.ui.cart.CartActivity;
import com.lujianfei.productprice.ui.cart.CartEntity;

import java.util.List;

public class BottomCart extends RelativeLayout {

    TextView txt_cart;
    public BottomCart(Context context) {
        super(context);
        init(context);
    }

    public BottomCart(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public BottomCart(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(final Context context) {
        LayoutInflater.from(context).inflate(R.layout.widget_bottom_cart,this,true);
        txt_cart = getRootView().findViewById(R.id.txt_cart);
        txt_cart.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                context.startActivity(new Intent(context,CartActivity.class));
            }
        });
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        refreshQuantity();
    }

    public void refreshQuantity(){
        List<CartEntity> cartEntities = CartEntity.listAll(CartEntity.class);
        int totalQuantity = 0;
        if (null!=cartEntities) {
            for (CartEntity cartEntity:
                    cartEntities){
                totalQuantity += cartEntity.getQuantity();
            }
        }
        txt_cart.setText(""+totalQuantity);
    }
}
